# coding=utf-8
import download
from bs4 import BeautifulSoup
import config
import httpclient
import time

list = httpclient.sendJson("", config.getConfig("url", "findBrand"))

for l in list:
    time.sleep(10)
    print '------------开始'
    html = download.download("https://www.guazi.com" + l['brandUrl'], 2)
    soup = BeautifulSoup(html)
    num = 0
    brandId = l['brandId']
    zhongwen = '不限'
    lista = []
    for k in soup.find_all('a'):
        d = dict()
        kvalue = k.get('href', default=None)
        if kvalue is None:
            continue
        if k.string == zhongwen.decode('utf-8', 'strict'):
            num += 1
            if k['href'] == '/bj/buy/#bread':
                continue
        if num == 2:
            name = k.string.strip()
            href = k['href']
            d.setdefault("modelsName", name)
            d.setdefault("modelsUrl", href)
            d.setdefault("brandId", l['brandId'])
            lista.append(d)
    res = httpclient.sendJson(lista, config.getConfig("url", "insertModels"))
    if res is None:
        res = httpclient.sendJson(lista, config.getConfig("url", "insertModels"))
